#include "stdafx.h"
#include "wf_dimleader.h"

#include "pf_leader.h"


WF_DimLeader::WF_DimLeader(PF_Container& container,
                                       PF_GraphicView& graphicView)
        :PF_PreviewActionInterface("Draw leaders",
                           container, graphicView) {

    reset();
}



WF_DimLeader::~WF_DimLeader() {}


QAction* WF_DimLeader::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
	// tr("Leader")
    QAction* action = new QAction(tr("&Leader"), NULL);
	action->setIcon(QIcon(":/extui/dimleader.png"));
    //action->zetStatusTip(tr("Leader Dimension"));
    return action;
}

void WF_DimLeader::reset() {
    //data = PF_LineAttribute(PF_Vector(false), PF_Vector(false));
    //start = PF_Vector(false);
    //history.clear();
    points.clear();
}



void WF_DimLeader::init(int status) {
    PF_PreviewActionInterface::init(status);

    reset();
}



void WF_DimLeader::trigger() {
    PF_PreviewActionInterface::trigger();

    if (points.count()>0) {

        PF_Leader* leader = new PF_Leader(container, PF_LeaderAttribute(true));
        leader->setLayerToActive();
        leader->setPenToActive();

        for (int i = 0; i < points.size(); ++i) {
            leader->addVertex(points.at(i));
        }

        container->addEntity(leader);

        // upd. undo list:
        if (document!=NULL) {
            document->startUndoCycle();
            document->addUndoable(leader);
            document->endUndoCycle();
        }

        deletePreview();
        PF_Vector rz = graphicView->getRelativeZero();
		graphicView->redraw(LX_Define::RedrawDrawing);
        graphicView->moveRelativeZero(rz);
        //drawSnapper();

        PF_DEBUG->print("PF_ActionDimLeader::trigger(): leader added: %d",
                        leader->getId());
    }
}



void WF_DimLeader::mouseMoveEvent(QMouseEvent* e) {
    PF_DEBUG->print("PF_ActionDimLeader::mouseMoveEvent begin");

    PF_Vector mouse = snapPoint(e);
    if (getStatus()==SetEndpoint && !points.isEmpty()) {
        deletePreview();

        // fill in lines that were already set:
        PF_Vector last(false);
        for (int i = 0; i < points.size(); ++i) {
            PF_Vector v = points.at(i);
            if (last.valid) {
                preview->addEntity(new PF_Line(preview,
                                               PF_LineAttribute(last, v)));
            }
            last = v;
        }

        if ( !points.isEmpty() ) {
            PF_Vector p = points.last();
            preview->addEntity(new PF_Line(preview,
                                       PF_LineAttribute(p, mouse)));
        }
        drawPreview();
    }

    PF_DEBUG->print("PF_ActionDimLeader::mouseMoveEvent end");
}



void WF_DimLeader::mouseReleaseEvent(QMouseEvent* e) {
    if (e->button()==Qt::LeftButton) {
        PF_CoordinateEvent ce(snapPoint(e));
        coordinateEvent(&ce);
    } else if (e->button()==Qt::RightButton) {
        if (getStatus()==SetEndpoint) {
            trigger();
            reset();
            setStatus(SetStartpoint);
        } else {
            deletePreview();
            init(getStatus()-1);
        }
    }
}



void WF_DimLeader::keyPressEvent(QKeyEvent* e) {
    if (getStatus()==SetEndpoint && e->key()==Qt::Key_Enter) {
        trigger();
        reset();
        setStatus(SetStartpoint);
    }
}



void WF_DimLeader::coordinateEvent(PF_CoordinateEvent* e) {
    if (e==NULL) {
        return;
    }

    PF_Vector mouse = e->getCoordinate();

    switch (getStatus()) {
    case SetStartpoint:
        //data.startpoint = mouse;
        points.clear();
        points.append(mouse);
        //start = data.startpoint;
        setStatus(SetEndpoint);
        graphicView->moveRelativeZero(mouse);
        break;

    case SetEndpoint:
        //data.endpoint = mouse;
        points.append(mouse);
        //trigger();
        //data.startpoint = data.endpoint;
        graphicView->moveRelativeZero(mouse);
        break;

    default:
        break;
    }
}



void WF_DimLeader::commandEvent(PF_CommandEvent* e) {
    QString c = e->getCommand().toLower();

    if (checkCommand("help", c)) {
        if (PF_DIALOGFACTORY!=NULL) {
            PF_DIALOGFACTORY->commandMessage(msgAvailableCommands()
                                             + getAvailableCommands().join(", "));
        }
        return;
    }

    // enter to finish
    if (c=="") {
        trigger();
        reset();
        setStatus(SetStartpoint);
        //finish();
    }
}



QStringList WF_DimLeader::getAvailableCommands() {
    QStringList cmd;

    return cmd;
}



void WF_DimLeader::updateMouseButtonHints() {
    if (PF_DIALOGFACTORY!=NULL) {
        switch (getStatus()) {
        case SetStartpoint:
            PF_DIALOGFACTORY->updateMouseWidget(tr("Specify target point"),
                                                tr("Cancel"));
            break;
        case SetEndpoint:
            PF_DIALOGFACTORY->updateMouseWidget(tr("Specify next point"),
                                                tr("Finish"));
            break;
        default:
            PF_DIALOGFACTORY->updateMouseWidget("", "");
            break;
        }
    }
}


void WF_DimLeader::showOptions() {
    PF_ActionInterface::showOptions();

    //PF_DIALOGFACTORY->requestOptions(this, true);
}



void WF_DimLeader::hideOptions() {
    PF_ActionInterface::hideOptions();

    //PF_DIALOGFACTORY->requestOptions(this, false);
}


void WF_DimLeader::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::CadCursor);
}


void WF_DimLeader::updateToolBar() {
    if (PF_DIALOGFACTORY!=NULL) {
        if (!isFinished()) {
            PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSnap);
        } else {
            PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarDim);
        }
    }
}

// EOF
